﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_SEC_ROLE
// * CreateDate    : 2011/7/13 18:14:57
// * Creater       : CodeSmith
// * LastChangeDate: 2011/7/13 18:14:57
// * LastChanger   : 2011/7/13 18:14:57
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Sec
{
    /// <summary>
    /// SecRole
    /// </summary>
    public class SecRoleBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objSecRoleEntity">Entity</param>
        /// <returns>Entity</returns>
        public static SecRoleEntity GetSecRole(SecRoleEntity objSecRoleEntity)
        {
            return DataAccess.SelectSingle<SecRoleEntity>(objSecRoleEntity);
        }
        public static SecRoleEntity GetSecRoleByID(string roleID)
        {
            if (string.IsNullOrEmpty(roleID))
            {
                return null;
            }
            SecRoleEntity entity = new SecRoleEntity();
            entity.RoleId = roleID;
            return DataAccess.SelectSingle<SecRoleEntity>(entity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objSecRoleEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertSecRole(SecRoleEntity objSecRoleEntity)
        {
            return DataAccess.Insert(objSecRoleEntity);
        }

        public static bool InsertSecRole(List<SecRoleEntity> objSecRoleEntityList)
        {
            return DataAccess.Insert(objSecRoleEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objSecRoleEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateSecRole(SecRoleEntity objSecRoleEntity)
        {
            return DataAccess.Update(objSecRoleEntity);
        }

        public static bool UpdateSecRole(List<SecRoleEntity> objSecRoleEntityList)
        {
            return DataAccess.Update<SecRoleEntity>(objSecRoleEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objSecRoleEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteSecRole(SecRoleEntity objSecRoleEntity)
        {
            return DataAccess.Delete(objSecRoleEntity);
        }

        public static bool DeleteSecRole(List<SecRoleEntity> objSecRoleEntityList)
        {
            return DataAccess.Delete<SecRoleEntity>(objSecRoleEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetRolesAll()
        {
            return DataAccess.SelectAllDataSet<SecRoleEntity>();
        }
        public static DataSet GetDsAllRoles()
        {
            string sql = @"SELECT R.*, PR.ROLE_NAME AS PARENT_ROLE_NAME, P.POSITION_NAME
                            FROM SEC_ROLE R
                            LEFT JOIN SEC_ROLE PR ON PR.ROLE_ID = R.PARENT_ID
							LEFT JOIN MST_POSITION P ON P.POSITION_ID = R.POSITION_ID
                            ORDER BY R.ROLE_NAME";
            return DataAccess.SelectDataSet(sql);
        }
        public static List<SecRoleEntity> GetRolesAllList()
        {
            return DataAccess.SelectAll<SecRoleEntity>();
        }
        #endregion

        #region Functions
        public static bool RoleSetting(SecRoleEntity role, List<SecUserRoleEntity> listUser, List<SecRoleMenuEntity> listMenu, List<SecObjectRoleEntity> listObject)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    //Delete
                    string sql = "DELETE FROM SEC_USER_ROLE WHERE ROLE_ID = '" + role.RoleId + "'";
                    broker.ExecuteSQL(sql);
                    sql = "DELETE FROM SEC_ROLE_MENU WHERE ROLE_ID = '" + role.RoleId + "'";
                    broker.ExecuteSQL(sql);
                    sql = "DELETE FROM SEC_OBJECT_ROLE WHERE ROLE_ID = '" + role.RoleId + "'";
                    broker.ExecuteSQL(sql);
                    //Indert
                    DataAccess.Insert<SecUserRoleEntity>(listUser, broker);
                    DataAccess.Insert<SecRoleMenuEntity>(listMenu, broker);
                    DataAccess.Insert<SecObjectRoleEntity>(listObject, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static List<SecRoleEntity> GetChildrenRolesForID(string sRoleID,List<SecRoleEntity> result)
        {
            string sql = @"select * from sec_role where parent_id='"+sRoleID+"'";
            List<SecRoleEntity> roles = new List<SecRoleEntity>(); 
            roles=DataAccess.Select<SecRoleEntity>(sql);
            if (result == null)
            {
                result = new List<SecRoleEntity>();
            }
            result.AddRange(roles);
            if (roles.Count != 0)
            {
                foreach (SecRoleEntity role in roles)
                {
                    List<SecRoleEntity> children = new List<SecRoleEntity>();
                    GetChildrenRolesForID(role.RoleId, result);
                }
            }
            return result;
        }
        #endregion
    }


}

